{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[31mPokemon.csv\u001b[m\u001b[m        demo_duplicate.csv sales-funnel.xlsx\r\n",
      "apply_demo.csv     iris.csv           train.csv\r\n",
      "city_weather.csv   \u001b[31mmovie_metadata.csv\u001b[m\u001b[m usa_flights.csv\r\n"
     ]
    }
   ],
   "source": [
    "!ls ../homework/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "link = 'https://projects.fivethirtyeight.com/flights/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df = pd.read_csv('../homework/usa_flights.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(201664, 14)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>flight_date</th>\n",
       "      <th>unique_carrier</th>\n",
       "      <th>flight_num</th>\n",
       "      <th>origin</th>\n",
       "      <th>dest</th>\n",
       "      <th>arr_delay</th>\n",
       "      <th>cancelled</th>\n",
       "      <th>distance</th>\n",
       "      <th>carrier_delay</th>\n",
       "      <th>weather_delay</th>\n",
       "      <th>late_aircraft_delay</th>\n",
       "      <th>nas_delay</th>\n",
       "      <th>security_delay</th>\n",
       "      <th>actual_elapsed_time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>02/01/2015 0:00</td>\n",
       "      <td>AA</td>\n",
       "      <td>1</td>\n",
       "      <td>JFK</td>\n",
       "      <td>LAX</td>\n",
       "      <td>-19.0</td>\n",
       "      <td>0</td>\n",
       "      <td>2475</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>381.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>03/01/2015 0:00</td>\n",
       "      <td>AA</td>\n",
       "      <td>1</td>\n",
       "      <td>JFK</td>\n",
       "      <td>LAX</td>\n",
       "      <td>-39.0</td>\n",
       "      <td>0</td>\n",
       "      <td>2475</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>358.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>04/01/2015 0:00</td>\n",
       "      <td>AA</td>\n",
       "      <td>1</td>\n",
       "      <td>JFK</td>\n",
       "      <td>LAX</td>\n",
       "      <td>-12.0</td>\n",
       "      <td>0</td>\n",
       "      <td>2475</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>385.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>05/01/2015 0:00</td>\n",
       "      <td>AA</td>\n",
       "      <td>1</td>\n",
       "      <td>JFK</td>\n",
       "      <td>LAX</td>\n",
       "      <td>-8.0</td>\n",
       "      <td>0</td>\n",
       "      <td>2475</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>389.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>06/01/2015 0:00</td>\n",
       "      <td>AA</td>\n",
       "      <td>1</td>\n",
       "      <td>JFK</td>\n",
       "      <td>LAX</td>\n",
       "      <td>25.0</td>\n",
       "      <td>0</td>\n",
       "      <td>2475</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>25.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>424.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       flight_date unique_carrier  flight_num origin dest  arr_delay  \\\n",
       "0  02/01/2015 0:00             AA           1    JFK  LAX      -19.0   \n",
       "1  03/01/2015 0:00             AA           1    JFK  LAX      -39.0   \n",
       "2  04/01/2015 0:00             AA           1    JFK  LAX      -12.0   \n",
       "3  05/01/2015 0:00             AA           1    JFK  LAX       -8.0   \n",
       "4  06/01/2015 0:00             AA           1    JFK  LAX       25.0   \n",
       "\n",
       "   cancelled  distance  carrier_delay  weather_delay  late_aircraft_delay  \\\n",
       "0          0      2475            NaN            NaN                  NaN   \n",
       "1          0      2475            NaN            NaN                  NaN   \n",
       "2          0      2475            NaN            NaN                  NaN   \n",
       "3          0      2475            NaN            NaN                  NaN   \n",
       "4          0      2475            0.0            0.0                  0.0   \n",
       "\n",
       "   nas_delay  security_delay  actual_elapsed_time  \n",
       "0        NaN             NaN                381.0  \n",
       "1        NaN             NaN                358.0  \n",
       "2        NaN             NaN                385.0  \n",
       "3        NaN             NaN                389.0  \n",
       "4       25.0             0.0                424.0  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>flight_date</th>\n",
       "      <th>unique_carrier</th>\n",
       "      <th>flight_num</th>\n",
       "      <th>origin</th>\n",
       "      <th>dest</th>\n",
       "      <th>arr_delay</th>\n",
       "      <th>cancelled</th>\n",
       "      <th>distance</th>\n",
       "      <th>carrier_delay</th>\n",
       "      <th>weather_delay</th>\n",
       "      <th>late_aircraft_delay</th>\n",
       "      <th>nas_delay</th>\n",
       "      <th>security_delay</th>\n",
       "      <th>actual_elapsed_time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>201659</th>\n",
       "      <td>10/01/2015 0:00</td>\n",
       "      <td>NK</td>\n",
       "      <td>188</td>\n",
       "      <td>OAK</td>\n",
       "      <td>LAS</td>\n",
       "      <td>-16.0</td>\n",
       "      <td>0</td>\n",
       "      <td>407</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>77.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>201660</th>\n",
       "      <td>11/01/2015 0:00</td>\n",
       "      <td>NK</td>\n",
       "      <td>188</td>\n",
       "      <td>OAK</td>\n",
       "      <td>LAS</td>\n",
       "      <td>-4.0</td>\n",
       "      <td>0</td>\n",
       "      <td>407</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>87.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>201661</th>\n",
       "      <td>12/01/2015 0:00</td>\n",
       "      <td>NK</td>\n",
       "      <td>188</td>\n",
       "      <td>OAK</td>\n",
       "      <td>LAS</td>\n",
       "      <td>-7.0</td>\n",
       "      <td>0</td>\n",
       "      <td>407</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>82.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>201662</th>\n",
       "      <td>13/01/2015 0:00</td>\n",
       "      <td>NK</td>\n",
       "      <td>188</td>\n",
       "      <td>OAK</td>\n",
       "      <td>LAS</td>\n",
       "      <td>23.0</td>\n",
       "      <td>0</td>\n",
       "      <td>407</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>103.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>201663</th>\n",
       "      <td>14/01/2015 0:00</td>\n",
       "      <td>NK</td>\n",
       "      <td>188</td>\n",
       "      <td>OAK</td>\n",
       "      <td>LAS</td>\n",
       "      <td>-7.0</td>\n",
       "      <td>0</td>\n",
       "      <td>407</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>82.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            flight_date unique_carrier  flight_num origin dest  arr_delay  \\\n",
       "201659  10/01/2015 0:00             NK         188    OAK  LAS      -16.0   \n",
       "201660  11/01/2015 0:00             NK         188    OAK  LAS       -4.0   \n",
       "201661  12/01/2015 0:00             NK         188    OAK  LAS       -7.0   \n",
       "201662  13/01/2015 0:00             NK         188    OAK  LAS       23.0   \n",
       "201663  14/01/2015 0:00             NK         188    OAK  LAS       -7.0   \n",
       "\n",
       "        cancelled  distance  carrier_delay  weather_delay  \\\n",
       "201659          0       407            NaN            NaN   \n",
       "201660          0       407            NaN            NaN   \n",
       "201661          0       407            NaN            NaN   \n",
       "201662          0       407            3.0            0.0   \n",
       "201663          0       407            NaN            NaN   \n",
       "\n",
       "        late_aircraft_delay  nas_delay  security_delay  actual_elapsed_time  \n",
       "201659                  NaN        NaN             NaN                 77.0  \n",
       "201660                  NaN        NaN             NaN                 87.0  \n",
       "201661                  NaN        NaN             NaN                 82.0  \n",
       "201662                  0.0       20.0             0.0                103.0  \n",
       "201663                  NaN        NaN             NaN                 82.0  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.tail()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 获取延误时间最长top10的航空公司"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>flight_date</th>\n",
       "      <th>unique_carrier</th>\n",
       "      <th>flight_num</th>\n",
       "      <th>origin</th>\n",
       "      <th>dest</th>\n",
       "      <th>arr_delay</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>11073</th>\n",
       "      <td>11/01/2015 0:00</td>\n",
       "      <td>AA</td>\n",
       "      <td>1595</td>\n",
       "      <td>AUS</td>\n",
       "      <td>DFW</td>\n",
       "      <td>1444.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10214</th>\n",
       "      <td>13/01/2015 0:00</td>\n",
       "      <td>AA</td>\n",
       "      <td>1487</td>\n",
       "      <td>OMA</td>\n",
       "      <td>DFW</td>\n",
       "      <td>1392.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12430</th>\n",
       "      <td>03/01/2015 0:00</td>\n",
       "      <td>AA</td>\n",
       "      <td>1677</td>\n",
       "      <td>MEM</td>\n",
       "      <td>DFW</td>\n",
       "      <td>1384.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8443</th>\n",
       "      <td>04/01/2015 0:00</td>\n",
       "      <td>AA</td>\n",
       "      <td>1279</td>\n",
       "      <td>OMA</td>\n",
       "      <td>DFW</td>\n",
       "      <td>1237.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10328</th>\n",
       "      <td>05/01/2015 0:00</td>\n",
       "      <td>AA</td>\n",
       "      <td>1495</td>\n",
       "      <td>EGE</td>\n",
       "      <td>DFW</td>\n",
       "      <td>1187.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36570</th>\n",
       "      <td>04/01/2015 0:00</td>\n",
       "      <td>DL</td>\n",
       "      <td>1435</td>\n",
       "      <td>MIA</td>\n",
       "      <td>MSP</td>\n",
       "      <td>1174.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36495</th>\n",
       "      <td>04/01/2015 0:00</td>\n",
       "      <td>DL</td>\n",
       "      <td>1367</td>\n",
       "      <td>ROC</td>\n",
       "      <td>ATL</td>\n",
       "      <td>1138.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>59072</th>\n",
       "      <td>14/01/2015 0:00</td>\n",
       "      <td>DL</td>\n",
       "      <td>1687</td>\n",
       "      <td>SAN</td>\n",
       "      <td>MSP</td>\n",
       "      <td>1084.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32173</th>\n",
       "      <td>05/01/2015 0:00</td>\n",
       "      <td>AA</td>\n",
       "      <td>970</td>\n",
       "      <td>LAS</td>\n",
       "      <td>LAX</td>\n",
       "      <td>1042.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>56488</th>\n",
       "      <td>12/01/2015 0:00</td>\n",
       "      <td>DL</td>\n",
       "      <td>2117</td>\n",
       "      <td>ATL</td>\n",
       "      <td>COS</td>\n",
       "      <td>1016.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           flight_date unique_carrier  flight_num origin dest  arr_delay\n",
       "11073  11/01/2015 0:00             AA        1595    AUS  DFW     1444.0\n",
       "10214  13/01/2015 0:00             AA        1487    OMA  DFW     1392.0\n",
       "12430  03/01/2015 0:00             AA        1677    MEM  DFW     1384.0\n",
       "8443   04/01/2015 0:00             AA        1279    OMA  DFW     1237.0\n",
       "10328  05/01/2015 0:00             AA        1495    EGE  DFW     1187.0\n",
       "36570  04/01/2015 0:00             DL        1435    MIA  MSP     1174.0\n",
       "36495  04/01/2015 0:00             DL        1367    ROC  ATL     1138.0\n",
       "59072  14/01/2015 0:00             DL        1687    SAN  MSP     1084.0\n",
       "32173  05/01/2015 0:00             AA         970    LAS  LAX     1042.0\n",
       "56488  12/01/2015 0:00             DL        2117    ATL  COS     1016.0"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_values('arr_delay', ascending=False)[:10][['flight_date','unique_carrier','flight_num','origin','dest','arr_delay']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.计算延误和没有延误所占比例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    196873\n",
       "1      4791\n",
       "Name: cancelled, dtype: int64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['cancelled'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df['delayed'] = df['arr_delay'].apply(lambda x: x > 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>flight_date</th>\n",
       "      <th>unique_carrier</th>\n",
       "      <th>flight_num</th>\n",
       "      <th>origin</th>\n",
       "      <th>dest</th>\n",
       "      <th>arr_delay</th>\n",
       "      <th>cancelled</th>\n",
       "      <th>distance</th>\n",
       "      <th>carrier_delay</th>\n",
       "      <th>weather_delay</th>\n",
       "      <th>late_aircraft_delay</th>\n",
       "      <th>nas_delay</th>\n",
       "      <th>security_delay</th>\n",
       "      <th>actual_elapsed_time</th>\n",
       "      <th>delayed</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>02/01/2015 0:00</td>\n",
       "      <td>AA</td>\n",
       "      <td>1</td>\n",
       "      <td>JFK</td>\n",
       "      <td>LAX</td>\n",
       "      <td>-19.0</td>\n",
       "      <td>0</td>\n",
       "      <td>2475</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>381.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>03/01/2015 0:00</td>\n",
       "      <td>AA</td>\n",
       "      <td>1</td>\n",
       "      <td>JFK</td>\n",
       "      <td>LAX</td>\n",
       "      <td>-39.0</td>\n",
       "      <td>0</td>\n",
       "      <td>2475</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>358.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>04/01/2015 0:00</td>\n",
       "      <td>AA</td>\n",
       "      <td>1</td>\n",
       "      <td>JFK</td>\n",
       "      <td>LAX</td>\n",
       "      <td>-12.0</td>\n",
       "      <td>0</td>\n",
       "      <td>2475</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>385.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>05/01/2015 0:00</td>\n",
       "      <td>AA</td>\n",
       "      <td>1</td>\n",
       "      <td>JFK</td>\n",
       "      <td>LAX</td>\n",
       "      <td>-8.0</td>\n",
       "      <td>0</td>\n",
       "      <td>2475</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>389.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>06/01/2015 0:00</td>\n",
       "      <td>AA</td>\n",
       "      <td>1</td>\n",
       "      <td>JFK</td>\n",
       "      <td>LAX</td>\n",
       "      <td>25.0</td>\n",
       "      <td>0</td>\n",
       "      <td>2475</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>25.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>424.0</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       flight_date unique_carrier  flight_num origin dest  arr_delay  \\\n",
       "0  02/01/2015 0:00             AA           1    JFK  LAX      -19.0   \n",
       "1  03/01/2015 0:00             AA           1    JFK  LAX      -39.0   \n",
       "2  04/01/2015 0:00             AA           1    JFK  LAX      -12.0   \n",
       "3  05/01/2015 0:00             AA           1    JFK  LAX       -8.0   \n",
       "4  06/01/2015 0:00             AA           1    JFK  LAX       25.0   \n",
       "\n",
       "   cancelled  distance  carrier_delay  weather_delay  late_aircraft_delay  \\\n",
       "0          0      2475            NaN            NaN                  NaN   \n",
       "1          0      2475            NaN            NaN                  NaN   \n",
       "2          0      2475            NaN            NaN                  NaN   \n",
       "3          0      2475            NaN            NaN                  NaN   \n",
       "4          0      2475            0.0            0.0                  0.0   \n",
       "\n",
       "   nas_delay  security_delay  actual_elapsed_time  delayed  \n",
       "0        NaN             NaN                381.0    False  \n",
       "1        NaN             NaN                358.0    False  \n",
       "2        NaN             NaN                385.0    False  \n",
       "3        NaN             NaN                389.0    False  \n",
       "4       25.0             0.0                424.0     True  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "delay_data = df['delayed'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.48906597112027927"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "delay_data[1]/(delay_data[0] + delay_data[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.每一个航空公司延误的情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "delay_group = df.groupby(['unique_carrier','delayed'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pandas.core.groupby.DataFrameGroupBy object at 0x10d7c16a0>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "delay_group"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df_delay = delay_group.size().unstack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>delayed</th>\n",
       "      <th>False</th>\n",
       "      <th>True</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>unique_carrier</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>AA</th>\n",
       "      <td>8912</td>\n",
       "      <td>9841</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AS</th>\n",
       "      <td>3527</td>\n",
       "      <td>2104</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B6</th>\n",
       "      <td>4832</td>\n",
       "      <td>4401</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DL</th>\n",
       "      <td>17719</td>\n",
       "      <td>9803</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>EV</th>\n",
       "      <td>10596</td>\n",
       "      <td>11371</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F9</th>\n",
       "      <td>1103</td>\n",
       "      <td>1848</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>HA</th>\n",
       "      <td>1351</td>\n",
       "      <td>1354</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MQ</th>\n",
       "      <td>4692</td>\n",
       "      <td>8060</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NK</th>\n",
       "      <td>1550</td>\n",
       "      <td>2133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>OO</th>\n",
       "      <td>9977</td>\n",
       "      <td>10804</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UA</th>\n",
       "      <td>7885</td>\n",
       "      <td>8624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>US</th>\n",
       "      <td>7850</td>\n",
       "      <td>6353</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>VX</th>\n",
       "      <td>1254</td>\n",
       "      <td>781</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WN</th>\n",
       "      <td>21789</td>\n",
       "      <td>21150</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "delayed         False  True \n",
       "unique_carrier              \n",
       "AA               8912   9841\n",
       "AS               3527   2104\n",
       "B6               4832   4401\n",
       "DL              17719   9803\n",
       "EV              10596  11371\n",
       "F9               1103   1848\n",
       "HA               1351   1354\n",
       "MQ               4692   8060\n",
       "NK               1550   2133\n",
       "OO               9977  10804\n",
       "UA               7885   8624\n",
       "US               7850   6353\n",
       "VX               1254    781\n",
       "WN              21789  21150"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_delay"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x118e99208>"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_delay.plot(kind='barh', stacked=True, figsize=[16,6], colormap='winter')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAA7UAAAFpCAYAAABDOg9IAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xu4Z3VdN/z3hwEdEAlF8CbRBn08AHKIGTyhpaip5SnU\nBCm0K5/xTk20eCzL+xmozCzMPPBUcHsKTUQN1BTLUDtAHva2QZCDKGCOkiCgQoHC8Hn+2L/h3gzD\nzN7D3r/fXjOv13Xta//Wd/3WWu99XawL3qy1vqu6OwAAADBEO0w6AAAAAGwtpRYAAIDBUmoBAAAY\nLKUWAACAwVJqAQAAGCylFgAAgMFSagEAABgspRYAAIDBUmoBAAAYLKUWAACAwdpx0gG21v3ud79e\nsWLFpGMAAACwCKanp7/X3Xtu6XuDLbUrVqzI1NTUpGMAAACwCKrqm3P5ntuPAQAAGCylFgAAgMFS\nagEAABiswT5TOz2dVE06BQDMU5846QQAbOc6ayYdYUG5UgsAAMBgLVqpraq3VNWrZy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/090/SnJ2krOr\n6rtJnpvknI330d2nJDllZn+rhvkuIgAAgCVs2bJlOfDAA29fPuuss7JixYpNfvfKK6/MM5/5zFx4\n4YVjSndn45z9+PlJTuvul20YqKp/SvKEqvp6d39nNBPyQUm+MsZcAAAAS1LVwu6v53BpcOedd87a\ntWsX9sCLaJy3Hx+dmVf5zPaRJO9N8vGqujAzZfbWJO8YYy4AAAA248orr8wTnvCEHHrooTn00ENz\n3nnn3ek7X/3qV/OoRz0qhxxySA466KBcdtllSZL3ve99t4+/7GUvy/r16xc029iu1Hb3kzYx9rYk\nbxtXBgAAADbvpptuyiGHHJIk2XfffXPmmWdmr732yqc//eksX748l112WY4++uhMTU3dYbu//Mu/\nzHHHHZdjjjkmP/7xj7N+/fpcfPHF+eAHP5hzzz03O+20U17+8pfn/e9/f4499tgFyzvO248BAABY\n4jZ1+/Ett9ySV77ylVm7dm2WLVuWr33ta3fa7rGPfWze8IY3ZN26dTnyyCPz0Ic+NOecc06mp6dz\n2GGHJZkpzHvttdeC5h1sqV25MtnofwyQJFkz6QAAAMA25i1veUvuf//75/zzz89tt92W5cuX3+k7\nL3rRi/LoRz86n/jEJ/LzP//z+au/+qt0d1784hfnjW9846JlG/crfQAAABiYH/zgB9l7772zww47\n5LTTTtvkc7GXX355HvzgB+dVr3pVnvOc5+QrX/lKnvzkJ+fDH/5wrr766iTJddddl29+85sLmk2p\nBQAAYLNe/vKX573vfW8OPvjgXHLJJbnXve51p++cccYZeeQjH5lDDjkkF154YY499tjsv//++cM/\n/MP83M/9XA466KA89alPzVVXXbWg2arnMqfzErRq1are+MFkAACAIbv44ouz3377TTrG2G3q766q\n6e5etaXujmXuAAAHr0lEQVRtXakFAABgsJRaAAAABkupBQAAYLCUWgAAAAZLqQUAAGCwlFoAAAAG\na8dJBwAAAGBpuPbaa/PkJz85SfKf//mfWbZsWfbcc88kyRe/+MXc4x73mGS8TRpsqZ2eTqomnQLu\nhj5x0glgSeismXQEAFiyKgv734xb+vfuHnvskbVr1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kkvivGA71t2v6+59untFZnrC\nZ7r7l+O82KbtOOkA89Xdt1bVK5P8fZJlSd7V3V+dcCxYUFX1gSRPTHK/qlqXZE2SP05yRlX9WpJv\nJvmlJOnur1bVGUkuyswMsa/o7vWjXb08MzMp75zk7NFPMlMOTquqr2dmQqqjxvBnwd1xeJJfSXLB\n6PnBJPndOC/Yvu2d5L2jmVp3SHJGd/9dVf1bnBewMf++2IaV/6kAAADAUA3x9mMAAABIotQCAAAw\nYEotAAAAg6XUAgAAMFhKLQAAAIOl1AIAADBYSi0AAACDpdQCAAAwWP8/P1cOMswu15wAAAAASUVO\nRK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x118ea1f98>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. 透视表功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>unique_carrier</th>\n",
       "      <th>AA</th>\n",
       "      <th>AS</th>\n",
       "      <th>B6</th>\n",
       "      <th>DL</th>\n",
       "      <th>EV</th>\n",
       "      <th>F9</th>\n",
       "      <th>HA</th>\n",
       "      <th>MQ</th>\n",
       "      <th>NK</th>\n",
       "      <th>OO</th>\n",
       "      <th>UA</th>\n",
       "      <th>US</th>\n",
       "      <th>VX</th>\n",
       "      <th>WN</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>flight_date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>02/01/2015 0:00</th>\n",
       "      <td>1545</td>\n",
       "      <td>477</td>\n",
       "      <td>759</td>\n",
       "      <td>2271</td>\n",
       "      <td>1824</td>\n",
       "      <td>254</td>\n",
       "      <td>224</td>\n",
       "      <td>1046</td>\n",
       "      <td>287</td>\n",
       "      <td>1763</td>\n",
       "      <td>1420</td>\n",
       "      <td>1177</td>\n",
       "      <td>176</td>\n",
       "      <td>3518</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>03/01/2015 0:00</th>\n",
       "      <td>1453</td>\n",
       "      <td>449</td>\n",
       "      <td>711</td>\n",
       "      <td>2031</td>\n",
       "      <td>1744</td>\n",
       "      <td>192</td>\n",
       "      <td>202</td>\n",
       "      <td>937</td>\n",
       "      <td>285</td>\n",
       "      <td>1681</td>\n",
       "      <td>1233</td>\n",
       "      <td>1028</td>\n",
       "      <td>160</td>\n",
       "      <td>3328</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>04/01/2015 0:00</th>\n",
       "      <td>1534</td>\n",
       "      <td>458</td>\n",
       "      <td>759</td>\n",
       "      <td>2258</td>\n",
       "      <td>1833</td>\n",
       "      <td>249</td>\n",
       "      <td>206</td>\n",
       "      <td>1027</td>\n",
       "      <td>284</td>\n",
       "      <td>1731</td>\n",
       "      <td>1283</td>\n",
       "      <td>1158</td>\n",
       "      <td>169</td>\n",
       "      <td>3403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>05/01/2015 0:00</th>\n",
       "      <td>1532</td>\n",
       "      <td>433</td>\n",
       "      <td>754</td>\n",
       "      <td>2212</td>\n",
       "      <td>1811</td>\n",
       "      <td>264</td>\n",
       "      <td>209</td>\n",
       "      <td>1039</td>\n",
       "      <td>288</td>\n",
       "      <td>1737</td>\n",
       "      <td>1432</td>\n",
       "      <td>1157</td>\n",
       "      <td>174</td>\n",
       "      <td>3506</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>06/01/2015 0:00</th>\n",
       "      <td>1400</td>\n",
       "      <td>415</td>\n",
       "      <td>692</td>\n",
       "      <td>2054</td>\n",
       "      <td>1686</td>\n",
       "      <td>249</td>\n",
       "      <td>202</td>\n",
       "      <td>966</td>\n",
       "      <td>279</td>\n",
       "      <td>1527</td>\n",
       "      <td>1294</td>\n",
       "      <td>1003</td>\n",
       "      <td>152</td>\n",
       "      <td>3396</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "unique_carrier     AA   AS   B6    DL    EV   F9   HA    MQ   NK    OO    UA  \\\n",
       "flight_date                                                                    \n",
       "02/01/2015 0:00  1545  477  759  2271  1824  254  224  1046  287  1763  1420   \n",
       "03/01/2015 0:00  1453  449  711  2031  1744  192  202   937  285  1681  1233   \n",
       "04/01/2015 0:00  1534  458  759  2258  1833  249  206  1027  284  1731  1283   \n",
       "05/01/2015 0:00  1532  433  754  2212  1811  264  209  1039  288  1737  1432   \n",
       "06/01/2015 0:00  1400  415  692  2054  1686  249  202   966  279  1527  1294   \n",
       "\n",
       "unique_carrier     US   VX    WN  \n",
       "flight_date                       \n",
       "02/01/2015 0:00  1177  176  3518  \n",
       "03/01/2015 0:00  1028  160  3328  \n",
       "04/01/2015 0:00  1158  169  3403  \n",
       "05/01/2015 0:00  1157  174  3506  \n",
       "06/01/2015 0:00  1003  152  3396  "
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flights_by_carrier = df.pivot_table(index='flight_date', columns='unique_carrier', values='flight_num', aggfunc='count')\n",
    "flights_by_carrier.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
